package view.plugin
{
	//桌子列表
	
	import event.UserSitEvent;
	
	import fl.containers.ScrollPane;
	import fl.controls.ScrollBarDirection;
	import fl.events.ScrollEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import flash3.framework.Singleton;
	import flash3.net.NetFacade;
	
	import global.Define;
	import global.Resource;
	
	import model.*;
	
	import net.*;
	
	import util.RandomUtil;
	
	import view.plugin.TableList;
	
	import vo.*;
	
	public class TableList extends ScrollPane
	{
		private var tableList:Array = [];
		private var mainSprite:Sprite;
		private var chairList:Array = [];
		
		private static const OFFSET_X:Number = 200;
		private static const OFFSET_Y:Number = 175;
		private static const COUNT_X:Number = 4;
		
		private var timer:Timer;
		public function TableList()
		{
			super();
			this.width = 816;
			this.height = 520;
			
			this.setStyle("upSkin", new MovieClip);
			this.verticalScrollPolicy = "on";
			
			var mainSprite:Sprite = new Sprite;
			var tableCount:uint = MeInfoModel.i.serverInfo.wTableCount;
			var tableCls:Class = Resource.i.getClass("seltable","Table");
			for(var i:uint = 0; i < tableCount; i++)
			{
				var mc:MovieClip = new tableCls;
				mc.x = OFFSET_X * (uint(i % COUNT_X));
				mc.y = OFFSET_Y * (uint(i / COUNT_X));
				mc.avatar1.buttonMode = true;
				mc.avatar2.buttonMode = true;
				mc.avatar3.buttonMode = true;
				mc.txtTableID.text = "-" + (i+1) + "-";
				mainSprite.addChild(mc);
				chairList.push(mc.avatar1,mc.avatar2,mc.avatar3);
				mc.avatar1.addEventListener(MouseEvent.CLICK, onClickAvatar);
				mc.avatar2.addEventListener(MouseEvent.CLICK, onClickAvatar);
				mc.avatar3.addEventListener(MouseEvent.CLICK, onClickAvatar);
				tableList.push(mc);
			}
			this.source = mainSprite;
			
			timer = new Timer(2000);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveStage);
		}
		public function onAddStage(evt:Event):void
		{
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			this.verticalScrollPosition = 0;
			addEventListener(ScrollEvent.SCROLL, scrollHandler);
		}
		
		
		
		public function scrollHandler(evt:ScrollEvent):void {
//			var sp:ScrollPane = evt.target as ScrollPane;
//			switch (evt.direction) {
//				case ScrollBarDirection.HORIZONTAL:
//					trace(evt.direction + ":", sp.horizontalScrollPosition, "of", sp.maxHorizontalScrollPosition);
//					break;
//				case ScrollBarDirection.VERTICAL:
//					trace(evt.direction + ":", sp.verticalScrollPosition, "of", sp.maxVerticalScrollPosition);
//					break;
//			}
		}
		

		
		public function onRemoveStage(evt:Event):void
		{
			timer.removeEventListener(TimerEvent.TIMER, onTimer);
		}
		private function onClickAvatar(evt:MouseEvent):void
		{
			for(var j:uint = 0; j < chairList.length; j++)
			{
				if(chairList[j] == evt.target)
				{
					var tableID:uint = uint(j / Define.GAME_PLAYER);
					var chairID:uint = uint(j % Define.GAME_PLAYER);
					var tableInfo:TableInfo = TableStatusModel.i.value(tableID) as TableInfo;
					var userSit:UserSit = new UserSit();
					userSit.wSiteID = tableInfo.wSiteID;
					userSit.wTableID = tableID;
					userSit.wChairID = chairID;
					userSit.szTablePass = tableInfo.szTablePass;
					var userEvent:UserSitEvent = new UserSitEvent(UserSitEvent.USER_SIT);
					userEvent.userSit = userSit;
					dispatchEvent(userEvent);
					break;
				}
			}
		}
		private function onTimer(evt:TimerEvent):void
		{
			updateData();
		}
		public function getTableView(tableID:uint):MovieClip
		{
			return tableList[tableID];
		}
		public function getChairView(tableID:uint, chairID:uint):Object
		{
			var tableView:MovieClip = tableList[tableID];
			var obj:Object = {};
			if(chairID == 0)
			{
				obj.avatar = tableView.avatar1;
				obj.name = tableView.txtName1;
				obj.hand = tableView.hand1;
			}
			else if(chairID == 1)
			{
				obj.avatar = tableView.avatar2;
				obj.name = tableView.txtName2;
				obj.hand = tableView.hand2;
			}
			else if(chairID == 2)
			{
				obj.avatar = tableView.avatar3;
				obj.name = tableView.txtName3;
				obj.hand = tableView.hand3;
			}
			return obj;
		}
		public function updateData():void
		{
			if(!MeInfoModel.i.serverInfo) return;
			var tableCount:uint = MeInfoModel.i.serverInfo.wTableCount;
			var tableInfo:TableInfo = null;
			for(var i:uint = 0; i < tableCount; i++)
			{
				tableInfo = TableStatusModel.i.value(i) as TableInfo;
				if(!tableInfo) 
					throw new Error("桌子:" + i + "为空！");
				updateTable(tableInfo);
			}
		}
		
		public function updateTable(tableInfo:TableInfo):void
		{
			var tableView:MovieClip = tableList[tableInfo.wTableID];
			if(!tableView) 
				throw new Error("桌子:" + tableInfo.wTableID + " tableView为空！");
			tableView.gameState.visible = tableInfo.bPlayStatus == 1;
			for(var i:uint = 0; i < Define.GAME_PLAYER; i++)
			{
				updateChair(tableInfo, i, tableInfo.userStatus[i]);
			}
		}
		
		private function initChair(tableID:uint, chairID:uint):void
		{
			var chairView:Object = getChairView(tableID, chairID);
			chairView.avatar.gotoAndStop(1);
			chairView.name.text = "";
			chairView.hand.visible = false;
			var tableView:MovieClip = tableList[tableID];
			tableView.gameState.visible = false;
		}
		
		public function updateChair(tableInfo:TableInfo, chairID:uint, userStatus:UserStatus):void
		{
			if(!userStatus)
			{
				initChair(tableInfo.wTableID, chairID);
			}
			else
			{
				var chairView:Object = getChairView(tableInfo.wTableID, chairID);
				chairView.hand.visible = userStatus.cbUserStatus==UserStatus.STATE_READY && tableInfo.bPlayStatus!=1;
				var userInfo:UserInfo = UserInfoModel.i.value(userStatus.dwUserID);
				if(!userInfo) return;
				chairView.avatar.gotoAndStop(userInfo.wFaceID);
				chairView.name.text = userInfo.szAccounts;
			}
		}
		
		static public function get i():TableList
		{
			return Singleton.getInstanceOrCreate(TableList) as TableList;
		}
	}
}